//
//  Colum_TitleViewController.m
//  YinHang
//
//  Created by 张亚哲 on 14/10/31.
//  Copyright (c) 2014年 孙彦鹏. All rights reserved.
//

#import "Colum_TitleViewController.h"

@interface Colum_TitleViewController (){
    
    __weak IBOutlet UIScrollView *mainView;
    
    __weak IBOutlet UITextField *titleTextField;
    
    __weak IBOutlet UITextView *keywordTextField;
    
    __weak IBOutlet UITextView *introduceTextField;
    
    MBProgressHUD *postHud;
    //参数
    ASIFormDataRequest *getDataRequest;//获取数据请求
    ASIFormDataRequest *postDataRequest;//修改请求
    NSString *action;
    NSDictionary *paramsDict;
    NSString *idStr;//id
}

@end

@implementation Colum_TitleViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewDidLayoutSubviews{
    UIScreen *screen = [UIScreen mainScreen];
    mainView.frame = CGRectMake(0, 0, screen.bounds.size.width,IPHONE5 ? 469:381);
    mainView.contentSize = CGSizeMake(screen.bounds.size.width, 568);
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UIScreen *screen = [UIScreen mainScreen];
    self.view.frame = CGRectMake(0, 0, screen.bounds.size.width, screen.bounds.size.height - 64 - 35);
    UIButton *commitBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    commitBtn.frame = CGRectMake(60, introduceTextField.frame.origin.y + introduceTextField.frame.size.height + 20, 200, 30);
    commitBtn.layer.cornerRadius = 5.0;
    [commitBtn setTitle:@"提交" forState:UIControlStateNormal];
    commitBtn.backgroundColor = APP_COLOR;
    [commitBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [commitBtn addTarget:self action:@selector(commit:) forControlEvents:UIControlEventTouchUpInside];
    [mainView addSubview:commitBtn];
    [self getData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark IBAction Method
//提交修改
-(void)commit:(id)sender{
    [self postRequest];
}
#pragma mark Custom Method
-(void)getData{
    if (![Constants isExistenceNetwork]) {
        
    } else {
        if (!postHud || postHud.hidden == YES) {
            // 提示正在加载数据...
            postHud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            postHud.labelText = @"正在加载，请稍后...";
        }
        
        getDataRequest = [[ASIFormDataRequest alloc]initWithURL:[NSURL URLWithString:API_STR]];
        getDataRequest.requestMethod = @"POST";
        // 设置超时时间
        getDataRequest.timeOutSeconds = 60;
        getDataRequest.delegate = self;
        // 设置参数
        action = @"DaohangLanmumain3";
        paramsDict = [NSDictionary dictionary];
        paramsDict = @{@"uid":UNAME};//uid会员名
        NSString *paramsStr = [NSString JSONString:paramsDict];
        NSString *result = [NSString stringWithFormat:TEMPLATE_PARAMSTR, action,paramsStr];
        
        NSData *paramsData = [result toGBKEncodingData];
        getDataRequest.postBody = [paramsData mutableCopy];
        
        [getDataRequest startAsynchronous];
    }
}

-(void)postRequest{
    if (![Constants isExistenceNetwork]) {
        
    } else {
        if (!postHud || postHud.hidden == YES) {
            // 提示正在加载数据...
            postHud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            postHud.labelText = @"正在修改，请稍后...";
        }
        
        postDataRequest = [[ASIFormDataRequest alloc]initWithURL:[NSURL URLWithString:API_STR]];
        postDataRequest.requestMethod = @"POST";
        // 设置超时时间
        postDataRequest.timeOutSeconds = 60;
        postDataRequest.delegate = self;
        // 设置参数
        action = @"DaohangLanmuupdate";
        paramsDict = [NSDictionary dictionary];
        paramsDict = @{@"uid":UNAME,@"Title":titleTextField.text,@"Keywords":keywordTextField.text,@"Contents":introduceTextField.text,@"id":idStr};//uid会员名
        NSString *paramsStr = [NSString JSONString:paramsDict];
        NSString *result = [NSString stringWithFormat:TEMPLATE_PARAMSTR, action,paramsStr];
        
        NSData *paramsData = [result toGBKEncodingData];
        postDataRequest.postBody = [paramsData mutableCopy];
        
        [postDataRequest startAsynchronous];
    }
}
#pragma mark - ASIHttpRequestDelegate method

- (void)requestFinished:(ASIHTTPRequest *)request {
    NSLog(@"%@",request.responseString);
    postHud.hidden = YES;
    NSError *err;
    NSDictionary *result = [NSJSONSerialization JSONObjectWithData:[request.responseString dataForUTF8] options:NSJSONReadingAllowFragments error:&err];
    
    // 1. 是否解析有错误
    if (!err) {
        
        // 2. 判断错误码
        NSInteger errorCode = [result[@"errorCode"]integerValue];
        
        if (errorCode == 0) {
            postHud.hidden = YES;
            if (request == getDataRequest) {
                //[SVProgressHUD showErrorWithStatus:result[@"errorMsg"]];
                NSDictionary *info = result[@"info"];
                titleTextField.text = info[@"Title"];
                keywordTextField.text = info[@"Keywords"];
                introduceTextField.text = info[@"Contents"];
                idStr = info[@"ID"];
            }else{
                [SVProgressHUD showSuccessWithStatus:@"修改成功"];
            }
        }else{
            postHud.hidden = YES;
        }
    } else {
        postHud.hidden = YES;
        [SVProgressHUD showErrorWithStatus:@"数据解析出错！"];
    }
}

- (void)requestFailed:(ASIHTTPRequest *)request{
    postHud.hidden = YES;
    [SVProgressHUD showErrorWithStatus:@"服务器无法访问，请稍后再试!"];
}


-(void)dealloc{
    
    [getDataRequest cancel];
    postHud = nil;
    getDataRequest = nil;
    
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [titleTextField resignFirstResponder];
    [keywordTextField resignFirstResponder];
    [introduceTextField resignFirstResponder];
}

//刷新数据
-(void)reFreshData{
    [self getData];
}
@end
